package com.rao.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rao.guli.service.base.result.R;
import com.rao.guli.service.edu.entity.Teacher;
import com.rao.guli.service.edu.entity.query.TeacherQuery;
import com.rao.guli.service.edu.feign.OssFileService;
import com.rao.guli.service.edu.mapper.TeacherMapper;
import com.rao.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author rao
 * @since 2021-07-02
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private OssFileService ossFileService;

    @Override
    public IPage<Teacher> selectPage(Long page, Long limit, TeacherQuery teacherQuery) {

        Page<Teacher> pageParam = new Page<>(page,limit);
        // 设置条件：根据 sort 字段升序排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        // 如果无满足条件的教师信息，则默认为空，仅使用分页，并使用 sort 字段进行升序排序
        if (teacherQuery == null){
            return baseMapper.selectPage(pageParam,queryWrapper);
        }

        // 获取 teacherQuery 的属性值
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String joinDateBegin = teacherQuery.getJoinDateBegin();
        String joinDateEnd = teacherQuery.getJoinDateEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.likeRight("name",name);
        }

        if (level != null){
            queryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.gt("joinDateBegin",joinDateBegin);
        }

        if (!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.lt("joinDateEnd",joinDateEnd);
        }

        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        // 设置条件进行查询
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        // 根据 name 查询
        queryWrapper.select("name");
        // 如果 key 非空，则通过匹配 name 属性进行右模糊搜索
        if (!StringUtils.isEmpty(key)){
            queryWrapper.likeRight("name",key);
        }
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);

        return list;
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        if (teacher != null){

            String avatar = teacher.getAvatar();

            if (!StringUtils.isEmpty(avatar)){
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }
        }

        return false;
    }

}
